﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace PTA.Web
{
    /// <summary>
    /// 需要用户登录的页面基类
    /// </summary>
    public abstract class PageBaseLogin : PageBase
    {
        /// <summary>
        /// 获取登录页面
        /// </summary>
        [System.ComponentModel.Description("登录地址,当用户登录超时转到此页面.")]
        public virtual string LoginUrl
        {
            get
            {
                var loginurl = Config.GetAppSettings("LoginUrl");
                if (string.IsNullOrWhiteSpace(loginurl))
                {
                    if (System.IO.File.Exists(Server.MapPath("~/Login.aspx")))
                    {
                        loginurl = "~/Login.aspx";
                    }
                }
                if (string.IsNullOrWhiteSpace(loginurl))
                {
                    throw new Exception("未定义登录地址，请在web.config中设置【LoginUrl】");
                }
                return loginurl;
            }
        }

        [System.ComponentModel.Description("首页地址,当菜单权限检查不通过时转到这个页面.")]
        public virtual string IndexUrl
        {
            get
            {
                return "~/Default.aspx";
            }
        }

        [System.ComponentModel.Description("是否检查URL菜单.")]
        protected virtual bool CheckUrl
        {
            get
            {
                return true;
            }
        }

        [System.ComponentModel.Description("检查用户的功能点信息")]
        protected static bool CheckUser(int privilege)
        {
            if (Auth.AuthManage.Instance != null)
            {
                return Auth.AuthManage.Instance.CheckAuth(privilege, PTA.Web.WebContext.CurrentUser);
            }
            return false;
        }

        [System.ComponentModel.Description("检查用户的菜单(URL)信息")]
        protected static bool CheckUser(string url)
        {
            if (Auth.AuthManage.Instance != null)
            {
                return Auth.AuthManage.Instance.CheckAuth(url, PTA.Web.WebContext.CurrentUser);
            }
            return false;
        }

        /// <summary>
        /// 页面初始化,在这个方法中检查用户是否登录,如果没有登录,跳转到登录页面
        /// </summary>
        protected void Page_Init(object sender,EventArgs e)
        {
            //在页面初始化时,检查用户是否登陆.如果没有登陆则跳转到登陆页面
            if (!WebContext.CurrentUser.IsLogin)
            {
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.Write(string.Format("<script type='text/javascript' language='javascript'>eval(\"window.top.location.href='{0}'\");</script>", ResolveUrl(LoginUrl)));
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }

            if (CheckUrl && Auth.AuthManage.Instance != null && (!CheckUser(Request.AppRelativeCurrentExecutionFilePath)))
            {
                Response.Clear();
                Response.Write(string.Format("<script type='text/javascript'>window.top.location.href='{0}';</script>", ResolveUrl(IndexUrl)));
                Response.End();
            }
        }
    }
}
